package com.spzx.order.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.common.rabbit.service.RabbitService;
import com.spzx.order.configure.DeadLetterMqConfig;
import com.spzx.order.configure.DelayedMqConfig;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Mq接口管理,用于测试")
@RestController
@RequestMapping("/mq")
public class MqController extends BaseController
{

    @Autowired
    RabbitService rabbitService;


    @Operation(summary = "发送消息")
    @GetMapping("/sendMessage")
    public AjaxResult sendMessage()
    {
        Boolean result = rabbitService.sendMsg(MqConst.EXCHANGE_TEST, MqConst.ROUTING_TEST, "test。。。");
        if(result){
            return success(result);
        }else{
            return error(result.toString());
        }
    }


    @Operation(summary = "发送确认消息")
    @GetMapping("/sendConfirmMessage")
    public AjaxResult sendConfirmMessage()
    {
        //rabbitService.sendMsg(MqConst.EXCHANGE_TEST, MqConst.ROUTING_CONFIRM, "hello, confirm");
        //测试：不存在的交换机名称："abc" 那么，需要进行消息重试
        //rabbitService.sendMsg("abc", MqConst.ROUTING_CONFIRM, "hello, confirm");
        //测试：不存在的队列绑定的路由key名称："xxx" 那么，找不到任何队列，会执行回退回调函数。
        rabbitService.sendMsg(MqConst.EXCHANGE_TEST, "xxx", "hello, confirm");
        return success();
    }



    /**
     * 消息发送延迟消息：基于死信实现
     */
    @Operation(summary = "发送延迟消息：基于死信实现")
    @GetMapping("/sendDeadLetterMsg")
    public AjaxResult sendDeadLetterMsg() {
        rabbitService.sendMsg(DeadLetterMqConfig.exchange_dead, DeadLetterMqConfig.routing_dead_1, "我是延迟消息");
        return success();
    }


    //延迟插件发送延迟消息，会执行回退处理，导致重试。这是插件自身的问题。
    //解决方案：
    // 1.禁止重复
    @Operation(summary = "发送延迟消息：基于延迟插件")
    @GetMapping("/sendDelayMsg")
    public AjaxResult sendDelayMsg() {
        //调用工具方法发送延迟消息
        int delayTime = 20; //10秒
        rabbitService.sendDelayMessage(DelayedMqConfig.exchange_delay, DelayedMqConfig.routing_delay, "我是延迟消息", delayTime);
        return success();
    }
}